import hashlib
import random
import time


def get_common_params(app_key, app_secret):
    m2 = ''.join(random.SystemRandom().choice('0123456789') for _ in range(32))
    sign_ts = str(int(time.time()))
    sign_no = ''.join(random.SystemRandom().choice('0123456789') for _ in range(20))
    to_be_hashed = f"{app_key}{m2}{sign_ts}{sign_no}{app_secret}".encode('utf-8')
    sign = hashlib.md5(to_be_hashed).hexdigest()

    common_params_map = {
        "appkey": app_key,
        "sign_ts": sign_ts,
        "sign_no": sign_no,
        "m2": m2,
        "sign": sign
    }

    return '&'.join([f"{key}={value}" for key, value in common_params_map.items()])


def get_product_device_params(product_key, device_name):
    body_params_map = {
        "product_key": product_key,
        "device_name": device_name
    }
    return body_params_map


def get_auth_header(pid):
    header_params = {
        "Authorization": f"qid {pid}"
    }
    return header_params
